<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>快递来了</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>



	<div class="container">
		<div id="ztc2pj" class="container">
			<ul id="myTab" class="nav nav-tabs">
				<li class="active"><a href="#waitehandle" data-toggle="tab">未取件</a></li>
				<li><a href="#alreadyhandle" data-toggle="tab">已取件</a></li>
			</ul>
			<div id="myTabContent" class="tab-content">
				<div class="tab-pane fade in active" id="waitehandle">
					<div class="row" style="padding-top: 5px">
						<div class="col-sm-12">
							<div id="toolbar" class="hidden-xs">
								<button id="add" type="button" class="btn" style="margin: 10;color:black;">
									添加</button>
							</div>
							<table id="mytable"></table>
						</div>
					</div>
				</div>
				<div class="tab-pane fade" id="alreadyhandle">
					<div class="row" style="padding-top: 5px">
						<div class="col-sm-12">
							<table id="tableAlready"></table>
						</div>
					</div>
				</div>
			</div>
		</div>

	</div>

	<div class="modal fade" id="addModal" style="z-index:9999">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">新入快递</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<label for="ownerPhone">收件人手机号</label> <input type="text"
							class="form-control" id="ownerPhone" placeholder="收件人手机号">
					</div>
					<div class="form-group">
						<label for="packageType">哪家快递</label> <input type="text"
							class="form-control" id="packageType" placeholder="哪家快递">
					</div>
					<div class="form-group">
						<label for="packageCode">快递单号</label> <input type="text"
							class="form-control" id="packageCode" placeholder=" 快递单号">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" id="btn_save" data-dismiss="modal"
						class="btn btn-primary">保存</button>
				</div>
			</div>
		</div>
	</div>


	<script>
		function timeStamp2String(time) {
			var datetime = new Date();
			datetime.setTime(time);
			var year = datetime.getFullYear();
			var month = datetime.getMonth() + 1;
			var date = datetime.getDate();
			var hour = datetime.getHours();
			var minute = datetime.getMinutes();
			var second = datetime.getSeconds();
			var mseconds = datetime.getMilliseconds();
			return year + "-" + month + "-" + date + " " + hour + ":" + minute
					+ ":" + second;
		};

		$('#add').click(function() {
			$('#addModal').modal();
		});

		$('#btn_save').click(function() {
			var phone = $('#ownerPhone').val();
			var type = $('#packageType').val();
			var code = $('#packageCode').val();
			$.get("${pageContext.request.contextPath}/package/addPackage.ztc", {
				ownerPhone : phone,
				packageType : type,
				packageCode : code
			}, function(data) {
				if (data >= 1) {
					swal('添加', '添加成功', 'success');
					$("#mytable").bootstrapTable("showLoading");
					reTable();
					$("#mytable").bootstrapTable("hideLoading");
				} else {
					swal('添加', '添加失败', 'error');
				}
			});
		});

		function reTable() {
			$.get("${pageContext.request.contextPath}/package/getPackages.ztc", {},
					function(data) {
						$('#running').show();
						var data1 = new Array();
						for (var i = 0; i < data.length; i++) {
							if (data[i].deletedFlag == 1) {
								data1.push(data[i]);
								data.splice(i, 1);
								i--;
							}
						}
						$("#mytable").bootstrapTable('load', data);
						$("#tableAlready").bootstrapTable('load', data1);
						$('#running').hide();
					}, "json");
		}
		$(function() {
			$('#lipackage').addClass("active");
			$.get("${pageContext.request.contextPath}/package/getPackages.ztc", {},
					function(data) {
						$('#running').show();
						var data1 = new Array();
						for (var i = 0; i < data.length; i++) {
							if (data[i].deletedFlag == 1) {
								data1.push(data[i]);
								data.splice(i, 1);
								i--;
							}
						}
						initTable(data);
						initAlreadyTable(data1);
						$('#running').hide();
					}, "json");
			//setInterval('reTable()',5000);
		});

		function initTable(data) {
			$('#mytable')
					.bootstrapTable(
							{
								height : $(window).height() - 135,
								data : data,
								method : 'get',
								striped : true,
								cache : false,
								pagination : true,
								sortOrder : "asc",
								pageSize : 10,
								dataType : 'json',
								pageList : [ 10, 20, 50, 100 ],
								sidePagination : "client",
								search : true,
								rowStyle : function(row,index){ 
									if((index+1)%2==0)
										return {css:{'color' : '#3c1414'}};
									return {css:{'color' : '#3c1414','background-color':'rgb(126, 137, 148)'}};
								},
								columns : [
										{
											field : "no",
											width : "5.5%",
											align : 'center',
											title : '序号',
											formatter : function(value, row,
													index) {
												return index + 1;
											}
										},
										{
											field : 'ower',
											align : 'center',
											title : ' 收件人 ',
											formatter : function(value, row,
													index) {
												return row.owner.name;
											}
										},
										{
											field : 'type',
											align : 'center',
											title : ' 快递'
										},
										{
											field : 'code',
											align : 'center',
											title : ' 快递单号  '
										},
										{
											field : 'menwei',
											align : 'center',
											title : ' 门卫  ',
											formatter : function(value, row,
													index) {
												return row.menwei.name;
											}
										},
										{
											field : 'firstTime',
											align : 'center',
											title : ' 到达时间  ',
											formatter : function(value, row,
													index) {
												return timeStamp2String(row.firstTime);
											}
										},
										{
											field : 'adviced',
											align : 'center',
											title : ' 邮件通知  ',
											formatter : function(value, row,
													index) {
												return value == 0 ? '未通知'
														: '已通知';
											}
										},
										{
											field : "action",
											width : "10%",
											title : '操作',
											align : 'center',
											valign : 'middle',
											cellStyle :function(value, row, index){
												if((index+1)%2==0)
													return {css:{'color' : '#3c1414'}};
												return {css:{'color' : '#3c1414','background-color':'rgb(126, 137, 148)'}};
											},
											formatter : function(value, row,
													index) {
												return '<a href="#" class="handle grid-link" style="color:blue">通知</a>|<a href="#" class="delete grid-link" style="color:blue">已取</a>';
											},
											events : {
												'click .handle' : function(e,
														value, row, index) {
													$
															.get(
																	"${pageContext.request.contextPath}/package/advice.ztc",
																	{
																		email : row.owner.email,
																		pId : row.id,
																		packageType : row.type,
																		packageCode : row.code
																	},
																	function(
																			data) {
																		swal("通知中");
																	});
												},
												'click .delete' : function(e,
														value, row, index) {
													$
															.get(
																	"${pageContext.request.contextPath}/package/filePackage.ztc",
																	{
																		id : row.id
																	},
																	function(
																			data) {
																		reTable();
																		swal("已归档");
																	});
												}
											}
										} ]
							});
		}

		function initAlreadyTable(data) {
			$('#tableAlready')
					.bootstrapTable(
							{
								height : $(window).height() - 100,
								data : data,
								method : 'get',
								striped : true,
								cache : false,
								pagination : true,
								sortOrder : "asc",
								pageSize : 14,
								dataType : 'json',
								pageList : [ 10, 20, 50, 100 ],
								sidePagination : "client",
								search : true,
								rowStyle : function(row,index){ 
									if((index+1)%2==0)
										return {css:{'color' : '#3c1414'}};
									return {css:{'color' : '#3c1414','background-color':'rgb(126, 137, 148)'}};
								},
								columns : [
										{
											field : "no",
											width : "5.5%",
											align : 'center',
											title : '序号',
											formatter : function(value, row,
													index) {
												return index + 1;
											}
										},
										{
											field : 'ower',
											align : 'center',
											title : ' 收件人 ',
											formatter : function(value, row,
													index) {
												return row.owner.name;
											}
										},
										{
											field : 'type',
											align : 'center',
											title : ' 快递'
										},
										{
											field : 'code',
											align : 'center',
											title : ' 快递单号  '
										},
										{
											field : 'menwei',
											align : 'center',
											title : ' 门卫  ',
											formatter : function(value, row,
													index) {
												return row.menwei.name;
											}
										},
										{
											field : 'firstTime',
											align : 'center',
											title : ' 到达时间  ',
											formatter : function(value, row,
													index) {
												return timeStamp2String(row.firstTime);
											}
										},
										{
											field : 'secondTime',
											align : 'center',
											title : ' 取走时间  ',
											formatter : function(value, row,
													index) {
												return row.secondTime == null ? '-'
														: timeStamp2String(row.secondTime);
											}
										} ]
							});
		}
	</script>

</body>

</html>